import subprocess
import time
import os

# 获取当前脚本所在的目录
script_dir = os.path.dirname(os.path.abspath(__file__))

# 结合脚本所在目录和相对路径来指定服务脚本的完整路径
server1_script = os.path.join(script_dir, "Server", "adb_http_server.py")
server2_script = os.path.join(script_dir, "Server", "match_http_server.py")
server3_script = os.path.join(script_dir, "Server", "ocr_http_server.py")

try:
    # 启动第一个服务
    process1 = subprocess.Popen(['python', server1_script])
    print(f"服务 {server1_script} 已启动，进程ID: {process1.pid}")

    # 启动第二个服务
    process2 = subprocess.Popen(['python', server2_script])
    print(f"服务 {server2_script} 已启动，进程ID: {process2.pid}")

    # 启动第三个服务
    process3 = subprocess.Popen(['python', server3_script])
    print(f"服务 {server3_script} 已启动，进程ID: {process3.pid}")

    # 等待一段时间，避免脚本立即退出
    while True:
        time.sleep(1)

except KeyboardInterrupt:
    print("接收到键盘中断信号，正在停止服务...")
    # 终止所有服务进程
    process1.terminate()
    process2.terminate()
    process3.terminate()

    # 等待进程结束
    process1.wait()
    process2.wait()
    process3.wait()

    print("所有服务已停止。")